﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;



public enum WeatherType
{
    [EnumAttirbute("无")]
    NONE = 0,
    [EnumAttirbute("雪")]
    SNOW = 1,
}

[ExecuteInEditMode]

public class EnvomentGlobalSetting : MonoBehaviour
{
    [Label("噪点")]
    public Texture2D noise;

    [Label("法线噪点")]
    public Texture2D bumpNoise;
    //public Camera MainCamera;
    //[Label("主光源改点光源")]
    private Transform mainPointLight;

    [Label("风的方向")]
    public Vector2 windDir = new Vector2(0.01f, 0.0f);
    [Label("风强度")]
    [Range(0, 1f)]
    public float windPower = 1f;
    private ComputeBuffer constantBuffer;
    public static int m_ConstantBufferID = Shader.PropertyToID("LchGlobalParams");



    [CNEnum(typeof(WeatherType), "天气类型")]
    public WeatherType weatherType = WeatherType.NONE;

    [Label("雪颜色(Alpha控制最大透明度)", "weatherType",1)]
    [ColorUsage(true,true)]
    public Color snowColor = Color.white;

    [Label("雪厚度", "weatherType", 1)]
    [Range(0f,1f)]
    public float snowPower = 0.0f;
    
    [Label("雪软边", "weatherType", 1)]
    [Range(0.01f, 0.3f)]
    public float snowThinness = 0.0f;

    [Label("雪粗糙度", "weatherType", 1)]
    [Range(0f, 1f)]
    public float snowSmoomthness = 0.3f;


    [Label("雪法线强度", "weatherType", 1)]
    [Range(0f, 1f)]
    public float snowNoiseScale = 0.3f;


    [Label("雪法线缩放", "weatherType", 1)]
    [Range(0f, 3f)]
    public float snowNoiseUvScale = 0.3f;

    public struct GlobalData
    {
        public Vector4 lchMainLightParams;
        public Vector4 windParams;
        public Vector4 weaterParams;
        public Vector4 weaterParams2;
        public Vector4 weaterParams3;

        // 编译时常量（5个Vector4 = 5*16 ）
        public const int Size = 5*16;
    }

    static GlobalData[] bufferDatas = new GlobalData[1];
    void InitBuffer()
    {
        // 创建 Constant Buffer
        //这里4字节对齐，只用四字节的参数
        if (null == constantBuffer)
            constantBuffer = new ComputeBuffer(1, GlobalData.Size, ComputeBufferType.Constant);

    }
    void OnEnable()
    {
        Shader.SetGlobalTexture("_Noise16X16",noise);
        Shader.SetGlobalTexture("_BumpNoise16X16", bumpNoise);
        //bumpNoise = (Texture2D)Resources.Load("noiseBump");
        InitBuffer();
        UpdateBuffer();
    }
 
    private void OnDisable()
    {
 
        clearRenderState();
        // 释放 Buffer
        if (constantBuffer != null)
        {
            constantBuffer.Release();
            constantBuffer = null;
        }
    }

    void clearRenderState()
    {
        InitBuffer();
        bufferDatas[0].lchMainLightParams = new Vector4(0f, 0f, 0f, 0.0f);
        // 将数据写入 Buffer
        constantBuffer.SetData(bufferDatas);
        // 绑定到 Shader
        Shader.SetGlobalConstantBuffer(m_ConstantBufferID, constantBuffer, 0, constantBuffer.stride);
    }
    void UpdateBuffer()
    {
        InitBuffer();

  
        bufferDatas[0].windParams = new Vector4(windDir.x, 100, windDir.y, windPower);
        if (mainPointLight)
        {
            var pos = mainPointLight.position;
            bufferDatas[0].lchMainLightParams = new Vector4(pos.x, pos.y, pos.z, 1.0f);
        }
        else
        {
            bufferDatas[0].lchMainLightParams = new Vector4(0f, 0f, 0f, 0.0f);
        }


        switch (weatherType)
        {
            case WeatherType.NONE:
                {
                    bufferDatas[0].weaterParams = Vector4.zero;

                } 
                break;
            case WeatherType.SNOW:
                {
                    bufferDatas[0].weaterParams.x = (float)weatherType;
                    float p = 1.0f - snowPower;
                    bufferDatas[0].weaterParams.y = p ;
                    bufferDatas[0].weaterParams.z =  snowThinness;
                    bufferDatas[0].weaterParams.w = snowSmoomthness;
                    bufferDatas[0].weaterParams2 = snowColor;

                    bufferDatas[0].weaterParams3.x = snowNoiseScale;
                    bufferDatas[0].weaterParams3.y = snowNoiseUvScale;


                }
                break;
        }
        // 将数据写入 Buffer
        constantBuffer.SetData(bufferDatas);
        // 绑定到 Shader
        Shader.SetGlobalConstantBuffer(m_ConstantBufferID, constantBuffer, 0, constantBuffer.stride);
    }

    void Update()
    {
 
#if UNITY_EDITOR

        if (null == noise)
        {
            noise = (Texture2D)Resources.Load("noise");
        }
        if (null == bumpNoise)
        {
            bumpNoise = (Texture2D)Resources.Load("noiseBump");
        }

#endif

        
        
        UpdateBuffer();
    }

}
     
